package com.dyzs.activity.word;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.dyzs.activity.CommonListActivity;
import com.dyzs.activity.R;
import com.dyzs.client.WordClient;
import com.dyzs.util.Pager;
public class WordListActivity extends CommonListActivity {
	
	private String cityId;	
	private Map map=null;	

	private EditText et;	
	private static final int[] menu_image_array = { R.drawable.up,R.drawable.down};
	private static final String[] menu_text_array = { "上一页", "下一页"};	

	
	
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.word_list);
		this.setTitle("导游词");		
		
		et=(EditText)this.findViewById(R.id.word_list_et);			
		if(getIntent().getStringExtra("searchTitle")!=null){			
			et.setText(getIntent().getStringExtra("searchTitle"));
		}
			
		emptyTV=(TextView)findViewById(android.R.id.empty);
		setGV();	
		if(getIntent().getStringExtra("cityId")!=null)
			cityId = getIntent().getStringExtra("cityId");
		else
			cityId="";
		update();
		
	}
	
	
	private void update() {
		showProgressDialog(getString(R.string.search_progress_title),getString(R.string.search_progress_content), true, true);
		new Thread(new Runnable() {
			public void run() {
				try {
					getDataFromService();
					WordListActivity.this.runOnUiThread(new Runnable() {
						public void run() {
							proDialog.dismiss();
							showList();
						}
					});
				} catch (Exception e) {
					onException(e);
				}
			}
		}).start();
	}

	public void onException(final Exception e) {
		WordListActivity.this.runOnUiThread(new Runnable() {
			public void run() {
				if (proDialog != null) {
					proDialog.dismiss();
				}
				emptyTV.setText(e.getMessage());						
			}
		});
	}

	private void getDataFromService() throws Exception {
		WordClient wordClient = new WordClient();
		String x = wordClient.updateList(cityId,et.getText().toString(),pager.getCurrentPage()+"");
		map= wordClient.pullListXml(x);		
	}

	private void showList() {	
		this.setTitle(map.get("totalNum")+"条记录");	
		if(map.get("totalNum").equals("0")){
			emptyTV.setText("暂无记录");
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this,
					(List<? extends Map<String, ?>>) map.get("list"),
					R.layout.common_list_item1, new String[] { "title"}, new int[] {R.id.common_list_item1_tv1});
		this.setListAdapter(simperAdapter);	
		pager=new Pager(Integer.valueOf(map.get("totalNum").toString()),pager.getCurrentPage());	
	}
	
	
	
	protected void onListItemClick(ListView l, View v, int position, long id) {		
		List abc= (List) map.get("list");	
		Intent intent = new Intent();
		intent.putExtra("wordId",((Map) abc.get(position)).get("wordId").toString());
		intent.putExtra("cityCode",cityId);
		intent.putExtra("showType","1");
		
		
		intent.setClass(WordListActivity.this, WordDetailActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
			
	
	private void setGV() {
		gv = (GridView) this.findViewById(R.id.word_list_gv);
		gv.setNumColumns(2);
		gv.setAdapter(getMenuAdapter(menu_text_array, menu_image_array));
		gv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {			
				switch (arg2) {
				case 0:
					if (1 == pager.getCurrentPage()) {// 上一页置为无效
						Toast.makeText(WordListActivity.this, "当前页为第一页",Toast.LENGTH_SHORT).show();
					}else{
						pager.doProvepage();
						update();
					}							
					break;
				case 1:
					if (pager.getTotalPage() == pager.getCurrentPage() || pager.getTotalPage() == 0) {
						Toast.makeText(WordListActivity.this, "当前页为最后一页",Toast.LENGTH_SHORT).show();
					}else{
						pager.doNextpage();
						update();
					}
					break;
				}
			}
		});
	}
	
	
	
	public void onClick(View v) {		
		pager.setCurrentPage(1);
		update();
	}
	
}
